package com.XiaoLongGroup.webtech.controller;/**
 * @author : SLCcccc
 * @date : 2023/5/12 11:05
 */

import com.XiaoLongGroup.webtech.VO.ArticleVO;
import com.XiaoLongGroup.webtech.VO.DeleteArticleVO;
import com.XiaoLongGroup.webtech.entity.Article;
import com.XiaoLongGroup.webtech.entity.User;
import com.XiaoLongGroup.webtech.service.ArticleService;
import com.XiaoLongGroup.webtech.utils.Page;
import com.XiaoLongGroup.webtech.utils.PageManage;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *@ClassName ArticleController
 *@Description TODO
 *@Author SLCcccc
 *@Date 2023/5/12 11:05
 *@Version 1.0
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    protected Map<String, Object> resultMap = new LinkedHashMap<>();

    @RequestMapping(value = "/getArticleList",method = RequestMethod.POST)
    @ResponseBody
    private Object getArticleList(Integer page, Integer limit, String search, String authorId){
        Page<Article> articlePage = null;
        if(StringUtils.isNoneBlank(authorId)){
            articlePage = articleService.getArticleListByNameLikeAndAuthorId(authorId,search,page,limit);
//            List<Article> articleList = articlePage.getRows();
//            for(Article article:articleList){
//                if(!article.getAuthor().getUserId().equals(authorId)){
//                    articleList.remove(article);
//                    articlePage.setTotal(articlePage.getTotal()-1);
//                }
//            }
//            articlePage.setRows(articleList);
        }else if(StringUtils.isNoneBlank(search)){
            articlePage = articleService.getArticleListByNameLike(search,page,limit);
        }else{
            articlePage = articleService.getArticleList(page,limit);
        }

        return PageManage.buildPage(articlePage);
    }

    @RequestMapping(value="/updateArticlePage",method = RequestMethod.GET)
    public ModelAndView updateArticle(int id){
        System.out.println(id);
        Article article = articleService.getArticleById(id);
        System.out.println("update");
        System.out.println(article);
        ModelAndView modelAndView=new ModelAndView("articleTable/edit");
        modelAndView.addObject("articleEdit", JSONArray.toJSONString(article));
        return modelAndView;
    }

    @RequestMapping(value ="/updateArticle",method = RequestMethod.POST)
    @ResponseBody
    public Object update(@RequestBody ArticleVO articleVO){
        System.out.println(articleVO);
        Article article = new Article();
        article.setAId(articleVO.getArticleId());
        article.setAName(articleVO.getArticleName());
        article.setADesc(articleVO.getArticleDesc());
        article.setABody(articleVO.getArticleBody());
        try{
            if(articleService.ArticleInfoChange(article)){
                resultMap.put("status",200);
                resultMap.put("message","修改成功");
            }else{
                resultMap.put("status",500);
                resultMap.put("message","用户不存在");
            }
        }catch(Exception e){
            resultMap.put("status",406);
            resultMap.put("message","系统出错");
            System.out.println(e.getMessage());
        }
        return resultMap;
    }

    @RequestMapping(value="/add",method = RequestMethod.GET)
    public ModelAndView addArticle(){
        return new ModelAndView("articleTable/add");
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editArticle(Integer articleId){
        Article articleEdit = articleService.getArticleById(articleId);
        ModelAndView modelAndView;
        modelAndView = new ModelAndView("articleTable/edit");
        modelAndView.addObject("articleEdit", JSONArray.toJSONString(articleEdit));
        return modelAndView;
    }

    @RequestMapping(value = "/addArticle",method = RequestMethod.POST)
    @ResponseBody
    public Object addArticle(@RequestBody ArticleVO articleVO){
        System.out.println(articleVO);
        Article article = new Article();
        article.setAName(articleVO.getArticleName());
        article.setABody(articleVO.getArticleBody());
        article.setADesc(articleVO.getArticleDesc());
        article.setAuthor(new User());
        article.getAuthor().setUserId(articleVO.getAuthorId());
        try{
            if(articleService.addArticle(article)){
                resultMap.put("status",200);
                resultMap.put("message","添加成功");
            }else{
                resultMap.put("status",500);
                resultMap.put("message","文章添加失败");
            }
        }catch(Exception e){
            resultMap.put("status",406);
            resultMap.put("message","系统出错");
            System.out.println(e.getMessage());
        }
        return resultMap;
    }

    @RequestMapping(value="/deleteArticle",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteArticle(@RequestBody DeleteArticleVO articleVO){
        try{
            if(articleService.deleteArticleById(articleVO.getArticleId())){
                resultMap.put("status",200);
                resultMap.put("message","删除成功");
            }else{
                resultMap.put("status",500);
                resultMap.put("message","系统出错");
            }
        }catch(Exception e){
            resultMap.put("status",406);
            resultMap.put("message","系统出错");
            System.out.println(e.getMessage());
        }
        return resultMap;
    }

    @RequestMapping(value = "/articleBatchDel",method = RequestMethod.POST)
    @ResponseBody
    public Object userBatchDel(@RequestBody List<DeleteArticleVO> articleVOS){
        System.out.println(articleVOS);
        boolean flag = true;
        List<Integer> errArticleIdList = null;
        for (DeleteArticleVO articleVO : articleVOS) {
            try{
                if(articleService.deleteArticleById(articleVO.getArticleId())){
                }else{
                    errArticleIdList.add(articleVO.getArticleId());
                    flag = false;
                }
            }catch(Exception e){
                resultMap.put("status",406);
                resultMap.put("message","系统出错");
            }
        }
        if(flag){
            resultMap.put("status",200);
            resultMap.put("message","删除成功");
        }else{
            resultMap.put("status",500);
            resultMap.put("message","系统出错,以下文章删除失败:" + errArticleIdList);
        }
        return resultMap;
    }
}
